using System;
using System.Collections;
using System.Text;

namespace LibFetion
{
    public enum SipMessageType
    {
        Register,
        SendMessage,
        Service,
        SearchBuddy
    }

    public class ConstValue
    {
        public static string FETION_CONFIG_URL = "http://nav.fetion.com.cn/nav/getsystemconfig.aspx";
        public static string FETION_LOGIN_URL = "https://nav.fetion.com.cn/ssiportal/SSIAppSignIn.aspx";
        public static string FETION_SIPP = "SIPP";
        public static string FETION_URL = "http://221.130.44.194/ht/sd.aspx";
        public static string FetionSipUrl = "fetion.com.cn";
        public static string SipF = "F";
        public static string SipI = "I";
        public static string SipM = "M";
        public static string SipN = "N";
        public static string SipQ = "Q";
        public static string SipR = "R";
        public static string SipS = "S";
        public static string SipT = "T";
        public static string SipA = "A";
        public static string ServerIP = "221.176.31.45";
        public static int ServerPort = 8080;
        public static string SipVersion = "SIP-C/2.0";
    }

    public class SipMessage
    {
        private readonly Hashtable fields;
        private string args="";
        private SipMessageType sipMessageType;

        public SipMessage(SipMessageType messageType)
        {
            sipMessageType = messageType;
            fields = new Hashtable();
        }

        public SipMessageType MessageType
        {
            get { return sipMessageType; }
            set { sipMessageType = value; }
        }

        public string Args
        {
            get { return args; }
            set { args = value; }
        }

        public string this[string key]
        {
            get { return fields[key].ToString(); }
        }

        public bool AddField(string key, string value)
        {
            bool success = true;
            try
            {
                fields.Add(key, value);
            }
            catch (Exception)
            {
                success = false;
            }
            return success;
        }

        public bool RemoveField(string key)
        {
            bool success = true;
            try
            {
                if (fields.ContainsKey(key))
                    fields.Remove(key);
            }
            catch (Exception)
            {
                success = false;
            }
            return success;
        }

        public string GetContent()
        {
            StringBuilder builder = new StringBuilder();
            switch (sipMessageType)
            {
                case SipMessageType.Register:
                    builder.AppendFormat("{0} ", ConstValue.SipR);
                    break;
                case SipMessageType.SendMessage:
                    builder.AppendFormat("{0} ", ConstValue.SipM);
                    break;
                case SipMessageType.Service:
                    builder.AppendFormat("{0} ", ConstValue.SipS);
                    break;
                default:
                    builder.AppendFormat("{0} ", ConstValue.SipM);
                    break;
            }
            builder.AppendFormat("{0} ", ConstValue.FetionSipUrl);
            builder.AppendFormat("{0}\r\n", ConstValue.SipVersion);
            IDictionaryEnumerator fieldEnumerator = fields.GetEnumerator();
            while (fieldEnumerator.MoveNext())
            {
                builder.AppendFormat("{0}: {1}\r\n", fieldEnumerator.Key, fieldEnumerator.Value);
            }
            builder.AppendFormat("L: {0}\r\n\r\n{1}", args.Length, args);
            return builder.ToString();
        }
    }
}